import { ref } from "vue";
import IPC from "../background/ipc";

export default function useLinkRepositories(like) {
  console.log(like);
  const linkList = ref([]);
  const getLinkList = async () => {
    console.log("--getLinkList-------------");
    console.log(like.value);
    let result = await IPC.invoke("link.list", { like: like.value });
    linkList.value = result.data;
  };

  const getLink = async (linkId) => {
    console.log("--getLink-------------");
    let get = await IPC.invoke("link.get", { id: linkId });
    console.log(get);
    return get.data;
  };

  const addLink = async (linkInfo) => {
    console.log("--addLink-------------");
    console.log(like.value);
    let add = await IPC.invoke("link.add", { linkInfo: linkInfo });
    console.log(add);
    let result = await IPC.invoke("link.list", { like: like.value });
    console.log(result);
    linkList.value = result.data;
  };

  const updateLink = async (linkInfo) => {
    console.log("--addLink-------------");
    console.log(like.value);
    let add = await IPC.invoke("link.update", { linkInfo: linkInfo });
    console.log(add);
    let result = await IPC.invoke("link.list", { like: like.value });
    console.log(result);
    linkList.value = result.data;
  };

  const removeLink = async (linkId) => {
    console.log(like);
    let remove = await IPC.invoke("link.remove", { id: linkId });
    console.log(remove);
    let result = await IPC.invoke("link.list", { like: like.value });
    console.log(result);
    linkList.value = result.data;
  };

  const resetLink = async () => {
    console.log(like);
    let remove = await IPC.invoke("link.reset", {});
    console.log(remove);
    let result = await IPC.invoke("link.list", { like: like.value });
    console.log(result);
    linkList.value = result.data;
  };

  //onMounted(getLinkList);
  //watch(like, getLinkList);

  return {
    linkList,
    getLinkList,
    getLink,
    addLink,
    updateLink,
    removeLink,
    resetLink,
  };
}
